﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class UAgentMacNewNGAdd : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] {  Permission.修改, Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UAgentMacList";
            }

        }
        /// <summary>
        /// 操作类型,ADD,UPDATE
        /// </summary>
        protected string Action
        {
            get {
                return GetRequestValue<string>("action", "add");
            }
        }
        protected int UASID
        {
            get
            {
                return GetRequestValue<int>("UASID", 0);
            }
        }

        private UAgentMacStockInfo _UAgentMacStock = null;
        protected UAgentMacStockInfo UAgentMacStockObj
        {
            get
            {
                if (_UAgentMacStock == null)
                    _UAgentMacStock = UAgentMacStockController.GetById(UASID);

                return _UAgentMacStock == null ? new UAgentMacStockInfo() { IntoStockDate="",MacType="",AgentName= HasPower(Permission.经销商平台_经销商)?base.User.Department:""} : _UAgentMacStock;
            }
        }

        private List<UAgentMacNewNGInfo> _CurrUAgentMacNewNGInfo = null;
        /// <summary>
        /// 当前ng内容
        /// </summary>
        protected List<UAgentMacNewNGInfo> CurrUAgentMacNewNG
        {
            get
            {
                if (_CurrUAgentMacNewNGInfo == null)
                    _CurrUAgentMacNewNGInfo = UAgentMacStockController.UAgentMacNewNG_GetByUASID(UASID);
                return _CurrUAgentMacNewNGInfo;
            }
        }

        private List<UAgentMacNewTempleteInfo> _AllUAgentMacNewTemplete = null;
        /// <summary>
        /// 所有可选的NG内容
        /// </summary>
        protected List<UAgentMacNewTempleteInfo> AllUAgentMacNewTemplete
        {
            get
            {
                if (_AllUAgentMacNewTemplete == null)
                    _AllUAgentMacNewTemplete = UAgentMacStockController.UAgentMacNewTemplete_GetAll();
                return _AllUAgentMacNewTemplete;
            }
        }
        /// <summary>
        /// 是否已选中
        /// </summary>
        /// <param name="_TempID"></param>
        /// <returns></returns>
        protected string GetChecked(int _TempID)
        {
           // int TrainId = Convert.ToInt32(_TrainId);
            List<UAgentMacNewNGInfo> templist = CurrUAgentMacNewNG.FindAll(e => e.TempID == _TempID );
            return templist.Count > 0 ? "checked" : "";
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "update")
            {
                updatemenu();
            }

            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
            }

           
                this.rpMenu.DataSource = AllUAgentMacNewTemplete.FindAll(X=>X.ParTempID==0);
                this.rpMenu.DataBind();
           
        }

        protected string GetSubTrainHtml(object _ParTempID)
        {
            /*
                <tr>
                    <td><input type="checkbox" name="checkselected"  value="<%# Eval("TrainId")%>" <%# GetChecked(Eval("TrainId")) %>  /></td>
                    <td><%# Eval("TrainDept")%></td>
                    <td><%# Eval("TrainContent")%></td>
                    <td><input type="text"  size="3" name="ClassHour" value="<%# GetClassHour(Eval("TrainId"),Eval("ClassHour")) %>"  /></td>
                    <td><input type="text" size="3" name="Unit" value="<%# GetUnit(Eval("TrainId"),Eval("Unit")) %>"  /></td>
                </tr>
             */

            int ParTempID =Convert.ToInt32( _ParTempID);
            string returnstr = "";
            if (Action == "add")
            {
                returnstr=UAgentMacStockController.DC.Get<string>(UAgentMacStockController.GetDataCacheKey(UAgentMacStockController.UAgentMacNewNGAddKeyByParId, ParTempID));
                if (!string.IsNullOrEmpty(returnstr))
                {
                    return returnstr;
                }
            }

            List<UAgentMacNewTempleteInfo> list = AllUAgentMacNewTemplete.Where<UAgentMacNewTempleteInfo>(e => e.ParTempID == ParTempID).ToList();

            StringBuilder sb = new StringBuilder();
            foreach (UAgentMacNewTempleteInfo itm in list)
            {
                UAgentMacNewNGInfo selObj = CurrUAgentMacNewNG.FirstOrDefault<UAgentMacNewNGInfo>(e => e.TempID == itm.TempID);
                selObj = selObj == null ? new UAgentMacNewNGInfo() : selObj;
                sb.AppendFormat(@"
                 <tr>
                    <td align='right' style=' text-align:right;'>
                     <input type='hidden' name='TempID'  value='{0}'/>
                     <input type='checkbox' name='checkselected' value='{0}' {1}/>
                    </td>                   
                    <td>{2}</td>
                    <td><input type='text'   name='NGRecord' value='{3}'  /></td>
                    <td><input type='text'   name='NGSolution' value='{4}'  /></td>
                    <td><a  class='imgUAgentMacNewNGAddimgShow' href='{5}'><img id='imgUAgentMacNewNGAddimgShow{0}' src='{5}' Width='30' Height='30'/></a></td>
                    <td>
                        <input type='hidden'   name='NGImg' id='imgUAgentMacNewNGAddNGImg{0}' value='{5}'  />
                        <input type='file' name='filedata' id='imgUAgentMacNewNGAdd{0}' upload='upload' clientId='imgUAgentMacNewNGAddimgShow{0},imgUAgentMacNewNGAddNGImg{0}' dirtype='6' foldername='{6}' onUploadSuccess='onAgentQltyUploadImgSuccess' fileTypeDesc='只能上传图片' fileTypeExts='*.jpg;*.gif;*.jpeg;*.png;*.bmp' fileSizeLimit='1MB'/>
                     
                    </td>
                    <td><a href='#' onclick=""onUAgentMacNewNGAddDeleteImg('imgUAgentMacNewNGAddNGImg{0}','imgUAgentMacNewNGAddimgShow{0}')"">删除</a></td>
                </tr>",
                      itm.TempID, selObj.ID > 0 ? "checked" : "", itm.Title, selObj.NGRecord, selObj.NGSolution, selObj.NGImg, User.Department
                );
            }

            if (Action == "add")
            {
                UAgentMacStockController.DC.Add(UAgentMacStockController.GetDataCacheKey(UAgentMacStockController.UAgentMacNewNGAddKeyByParId, ParTempID), sb.ToString());
            }

            return sb.ToString();
        }

        protected void addmenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] checkselecteds = GetRequestValue<string>("checkselected", "").Split(caSplit);
            string[] TempIDs = GetRequestValue<string>("TempID", "").Split(caSplit);
            string[] NGRecords = GetRequestValue<string>("NGRecord", "").Split(caSplit);
            string[] NGSolutions = GetRequestValue<string>("NGSolution", "").Split(caSplit);
            string[] NGImgs = GetRequestValue<string>("NGImg", "").Split(caSplit);

            UAgentMacStockInfo obj = Request.SerializableObject<UAgentMacStockInfo>();
            obj.AgentName = GetRequestValue<string>("dwz.orgLookup.AgentName", "");
            obj.CheckNew = true;
            obj.IsInIHISCE = false;

            List<UAgentMacNewNGInfo> list = new List<UAgentMacNewNGInfo>();
            for (int index = 0; index < checkselecteds.Count(); index++)
            {
                if (string.IsNullOrEmpty(checkselecteds[index]))
                    continue;

                UAgentMacNewNGInfo itm = new UAgentMacNewNGInfo();
                itm.TempID = Convert.ToInt32(checkselecteds[index]);
                itm.NGRecord = NGRecords[index];
                itm.NGSolution = NGSolutions[index];

                for (int tindex = 0; tindex < TempIDs.Count(); tindex++)
                {
                    if (Convert.ToInt32(TempIDs[tindex]) == itm.TempID)
                    {
                        itm.NGImg = NGImgs[tindex];
                    }
                }
                

                list.Add(itm);
            }

            int r= UAgentMacStockController.UAgentMacNewNG_Add(obj, list);
            if (r == -1)
            {
                ErrorJson("添加失败，此机号已存在系统，不能重复添加");
            }
            if (r > 0)
            {
                SuccessJson("成功保存");
            }
          

        }

        private void updatemenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] checkselecteds = GetRequestValue<string>("checkselected", "").Split(caSplit);
            string[] TempIDs = GetRequestValue<string>("TempID", "").Split(caSplit);
            string[] NGRecords = GetRequestValue<string>("NGRecord", "").Split(caSplit);
            string[] NGSolutions = GetRequestValue<string>("NGSolution", "").Split(caSplit);
            string[] NGImgs = GetRequestValue<string>("NGImg", "").Split(caSplit);

           // UAgentMacStockInfo obj = Request.SerializableObject<UAgentMacStockInfo>();
            UAgentMacStockObj.IntoStockDate = GetRequestValue<string>("IntoStockDate", "");
            UAgentMacStockObj.MacType = GetRequestValue<string>("MacType", "");
            UAgentMacStockObj.SerialNumber = GetRequestValue<string>("SerialNumber", "");
            UAgentMacStockObj.Timer = GetRequestValue<string>("Timer", "");
            UAgentMacStockObj.Antifreeze = GetRequestValue<string>("Antifreeze", "");
            UAgentMacStockObj.Diesel = GetRequestValue<string>("Diesel", "");
            UAgentMacStockObj.AccepPlace = GetRequestValue<string>("AccepPlace", "");
            UAgentMacStockObj.ScanImg = GetRequestValue<string>("ScanImg", "");
            List<UAgentMacNewNGInfo> list = new List<UAgentMacNewNGInfo>();
            for (int index = 0; index < checkselecteds.Count(); index++)
            {
                if (string.IsNullOrEmpty(checkselecteds[index]))
                    continue;
                UAgentMacNewNGInfo itm = new UAgentMacNewNGInfo();
                itm.TempID = Convert.ToInt32(checkselecteds[index]);
                itm.NGRecord = NGRecords[index];
                itm.NGSolution = NGSolutions[index];

                for (int tindex = 0; tindex < TempIDs.Count(); tindex++)
                {
                    if (Convert.ToInt32(TempIDs[tindex]) == itm.TempID)
                    {
                        itm.NGImg = NGImgs[tindex];
                    }
                }


                list.Add(itm);
            }

            bool r = UAgentMacStockController.UAgentMacNewNG_Update(UAgentMacStockObj, list);
            if (r )
            {
                SuccessJson("成功修改",this.DWZRef);
            }
            if (!r)
            {
                ErrorJson("修改失败");
                
            }
        }
    }
}